package net.comfycastle.musicplayer;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 첫 화면 이후 음악 재생 전까지의 리스트를 출력하는 액티비티
 * @author ricale
 *
 */
public class DetailActivity extends BaseActivity implements BaseListFragment.OnListItemClickListener {
	///////////
	//////////
	/////////   variable
	
	/** 현재 보여지고 있는 프래그먼트 */
	private BaseListFragment mFMain;
	
	///////////
	//////////
	/////////   lifecycle callback method
	
	/**
	 * 액티비티 생성 후의 콜백 메서드
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.detail);
		
		Intent intent = getIntent();
		String action = intent.getAction();
		
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		
		if(action.equals(MPApp.ACT_SHOW_LIST_ALBUMS)) {
			String kind = intent.getStringExtra(MPApp.ITT_GROUPKIND);
			String name = intent.getStringExtra(MPApp.ITT_GROUPNAME);
			mFMain = AlbumListFragment.newInstance(BaseListFragment.OBJECTID.ALBUM_INARTIST, kind, name);
			ft.add(R.id.detail_container_infofragment, TitleFragment.newInstance(mFMain.getTitleForWindow()));
		} else if(action.equals(MPApp.ACT_SHOW_LIST_INGROUP) || action.equals(MPApp.ACT_SHOW_PLAYLIST_INGROUP)) {
			String kind = intent.getStringExtra(MPApp.ITT_GROUPKIND);
			String name = intent.getStringExtra(MPApp.ITT_GROUPNAME);
			mFMain = SongListFragment.newInstance(BaseListFragment.OBJECTID.LIST_INGROUP, kind, name);
			ft.add(R.id.detail_container_infofragment, TitleFragment.newInstance(mFMain.getTitleForWindow()));
		} else { // (action.equals(MPApp.ACT_SHOW_LIST_INALBUM) || action.equals(MPApp.ACT_SHOW_PLAYLIST_INALBUM)) 
			String  name   = intent.getStringExtra(MPApp.ITT_GROUPNAME);
			String  artist = intent.getStringExtra(MPApp.ITT_TITLEARTIST);
			boolean isAA   = intent.getBooleanExtra(MPApp.ITT_ISALBUMARTIST, false);
			mFMain = InalbumFragment.newInstance(BaseListFragment.OBJECTID.LIST_INALBUM, name, artist, isAA);
			ft.add(R.id.detail_container_infofragment, InalbumInfoFragment.newInstance(name, artist, isAA));
		} // end if
		
		ft.add(R.id.detail_container_basefragment, mFMain).commit();
	} // end onCreate
	
	///////////
	//////////
	/////////   event listener & callback method
	
	/**
	 * 리스트 아이템 클릭 리스너 메서드.
	 * Fragment의 리스트 아이템이 클릭될 경우 해당 리스너에서 이 메서드를 호출한다.
	 */
	public void onItemClick(BaseListFragment.OBJECTID fragmentId, ListView l, View view, int position, long id) {
		Intent intent;
		
		String action = getIntent().getAction();
		
		if(action.equals(MPApp.ACT_SHOW_PLAYLIST_INGROUP) || action.equals(MPApp.ACT_SHOW_PLAYLIST_INALBUM)) {
		
			switch(fragmentId) {
			case LIST_INGROUP:
				intent = new Intent();
				intent.setAction(MPApp.ACT_PLAY);
				
				intent.putExtra(MPApp.ITT_GROUPKIND,  ((SongListFragment)mFMain).getKind());
				intent.putExtra(MPApp.ITT_GROUPNAME,  ((SongListFragment)mFMain).getName());
				intent.putExtra(MPApp.ITT_STARTTRACK, position);
				
				setResult(RESULT_OK, intent);
				finish();
				break;
			case LIST_INALBUM: default:
				intent = new Intent();
				intent.setAction(MPApp.ACT_PLAY);
				
				intent.putExtra(MPApp.ITT_GROUPKIND,     MPApp.TAG_ALBUM);
				intent.putExtra(MPApp.ITT_GROUPNAME,     ((InalbumFragment)mFMain).getName());
				intent.putExtra(MPApp.ITT_TITLEARTIST,   ((InalbumFragment)mFMain).getArtist());
				intent.putExtra(MPApp.ITT_ISALBUMARTIST, ((InalbumFragment)mFMain).getIsAA());
				intent.putExtra(MPApp.ITT_STARTTRACK,    position);
				
				setResult(RESULT_OK, intent);
				finish();
				break;
			} // end switch
			
		} else {

			switch(fragmentId) {
			case ALBUM_INARTIST:
				intent = new Intent(this, DetailActivity.class);
				if(position == 0) {
					intent.setAction(MPApp.ACT_SHOW_LIST_INGROUP);
					
					intent.putExtra(MPApp.ITT_GROUPKIND, ((AlbumListFragment)mFMain).getKind());
					intent.putExtra(MPApp.ITT_GROUPNAME, ((AlbumListFragment)mFMain).getName());
				} else {
					intent.setAction(MPApp.ACT_SHOW_LIST_INALBUM);
					
					TextView tvMain = (TextView)view.findViewById(R.id.listitem_albumlist_album);
					
					intent.putExtra(MPApp.ITT_GROUPNAME,     tvMain.getText());
					intent.putExtra(MPApp.ITT_TITLEARTIST,   ((AlbumListFragment)mFMain).getName());
					intent.putExtra(MPApp.ITT_ISALBUMARTIST, ((AlbumListFragment)mFMain).getKind().equals(MPApp.TAG_ALBUMARTIST));
				} // end if
				break;
			case LIST_INGROUP:
				intent = new Intent(this, PlayActivity.class);
				intent.setAction(MPApp.ACT_PLAY);
				
				intent.putExtra(MPApp.ITT_GROUPKIND,  ((SongListFragment)mFMain).getKind());
				intent.putExtra(MPApp.ITT_GROUPNAME,  ((SongListFragment)mFMain).getName());
				intent.putExtra(MPApp.ITT_STARTTRACK, position);
				break;
			case LIST_INALBUM: default:
				intent = new Intent(this, PlayActivity.class);
				intent.setAction(MPApp.ACT_PLAY);
				
				intent.putExtra(MPApp.ITT_GROUPKIND,     MPApp.TAG_ALBUM);
				intent.putExtra(MPApp.ITT_GROUPNAME,     ((InalbumFragment)mFMain).getName());
				intent.putExtra(MPApp.ITT_TITLEARTIST,   ((InalbumFragment)mFMain).getArtist());
				intent.putExtra(MPApp.ITT_ISALBUMARTIST, ((InalbumFragment)mFMain).getIsAA());
				intent.putExtra(MPApp.ITT_STARTTRACK,    position);
				break;
			} // end switch
			
			startActivity(intent);
		} // end if
	} // end onListFragmentItemClick
} // end DetailActivity